<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class m_laporan_resep extends CI_Model {

	public function __construct() {
		parent::__construct();
	}

	function get() {
		$sql = 'SELECT * FROM resep
		INNER JOIN kunjungan ON resep.KdKunjungan = kunjungan.KdKunjungan 
		INNER JOIN pasien ON kunjungan.KdPasien = pasien.KdPasien
		ORDER by KdResep DESC';
		return $sql;
	}

	function get_cari($tgl_awal,$tgl_akhir,$pasien,$obat) {
		if($pasien != ''){
			$where_pasien = "AND pasien.KdPasien = '".$pasien."'";
		}else{
			$where_pasien = "";
		}

		if($obat != ''){
			$where_obat = "AND obat_resep.KdObat = '".$obat."'";
		}else{
			$where_obat = "";
		}

		$sql = 'SELECT resep.KdResep, Nama, NmDokter, TglResep FROM resep 
		INNER JOIN obat_resep ON resep.KdResep = obat_resep.KdResep
		INNER JOIN kunjungan ON resep.KdKunjungan = kunjungan.KdKunjungan
		INNER JOIN pasien ON kunjungan.KdPasien = pasien.KdPasien
		WHERE TglResep between "'.$tgl_awal.'" AND "'.$tgl_akhir.'" 
		'.$where_pasien.'
		'.$where_obat.'
		GROUP by resep.KdResep
		ORDER by resep.KdResep DESC';
		return $sql;
	}

	function get_cari_non_tgl($petugas,$obat) {
		if($petugas != ''){
			$where_petugas = "AND rc_users.id = '".$petugas."'";
		}else{
			$where_petugas = "";
		}

		if($obat != ''){
			$where_obat = "AND detail_jual.KdObat = '".$obat."'";
		}else{
			$where_obat = "";
		}

		$sql = 'SELECT nickname, jual.KdJual, Tanggal, Keterangan, id_user FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user
		INNER JOIN detail_jual ON jual.KdJual = detail_jual.KdJual
		INNER JOIN obat ON detail_jual.KdObat = obat.KdObat
		WHERE jual.KdJual <> ""
		'.$where_petugas.'
		'.$where_kelompok.'
		'.$where_obat.'
		GROUP by KdJual
		ORDER by KdJual DESC';
		return $sql;
	}

	function get_obat() {
		$sql = 'SELECT * FROM obat';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_pasien() {
		$sql = 'SELECT KdPasien, Nama FROM pasien ORDER BY Nama';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_export_all() {
		$sql = 'SELECT * FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user ORDER by KdJual DESC';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_export($tgl_awal,$tgl_akhir,$petugas,$kelompok,$obat) {
		if($petugas != ''){
			$where_petugas = "AND rc_users.id = '".$petugas."'";
		}else{
			$where_petugas = "";
		}

		if($kelompok != ''){
			$where_kelompok = "AND obat.KdKelompok = '".$kelompok."'";
		}else{
			$where_kelompok = "";
		}

		if($obat != ''){
			$where_obat = "AND detail_jual.KdObat = '".$obat."'";
		}else{
			$where_obat = "";
		}

		$sql = 'SELECT nickname, jual.KdJual, Tanggal, Keterangan, id_user FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user
		INNER JOIN detail_jual ON jual.KdJual = detail_jual.KdJual
		INNER JOIN obat ON detail_jual.KdObat = obat.KdObat
		WHERE tanggal between "'.$tgl_awal.'" AND "'.$tgl_akhir.'" 
		'.$where_petugas.'
		'.$where_kelompok.'
		'.$where_obat.'
		GROUP by KdJual
		ORDER by KdJual DESC';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	function get_export_non_tgl($petugas,$kelompok,$obat) {
		if($petugas != ''){
			$where_petugas = "AND rc_users.id = '".$petugas."'";
		}else{
			$where_petugas = "";
		}

		if($kelompok != ''){
			$where_kelompok = "AND obat.KdKelompok = '".$kelompok."'";
		}else{
			$where_kelompok = "";
		}

		if($obat != ''){
			$where_obat = "AND detail_jual.KdObat = '".$obat."'";
		}else{
			$where_obat = "";
		}

		$sql = 'SELECT nickname, jual.KdJual, Tanggal, Keterangan, id_user FROM jual 
		INNER JOIN rc_users ON rc_users.id = jual.id_user
		INNER JOIN detail_jual ON jual.KdJual = detail_jual.KdJual
		INNER JOIN obat ON detail_jual.KdObat = obat.KdObat
		WHERE jual.KdJual <> ""
		'.$where_petugas.'
		'.$where_kelompok.'
		'.$where_obat.'
		GROUP by KdJual
		ORDER by KdJual DESC';
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}
}
